package cae.gui.util;

import java.awt.Image;
import java.awt.Toolkit;

import org.apache.log4j.Logger;

import cae.logic.util.CaeClipConfiguration;

import com.jeans.trayicon.WindowsTrayIcon;

public class CaeImage {
	
	public static final int DEFAULT = 0;
	public static final int MESSAGE = 1;
	private static Image defaultIcon;
	private static Thread swicherThread;
	private static Logger logger = Logger.getLogger(CaeImage.class);

	public static void setIcon(final WindowsTrayIcon icon, int iconNumber)
	{
		if(swicherThread != null)
			swicherThread.interrupt();
		
		String iconPath;
		
		if(defaultIcon == null)
		{
			iconPath = CaeClipConfiguration.getInstance().getSProp(CaeClipConfiguration.ICON_FILE);
			defaultIcon = Toolkit.getDefaultToolkit().getImage(iconPath);
		}
		
		try {
			Image img = null;
			switch (iconNumber) 
			{
				case DEFAULT:
					icon.setImage(defaultIcon,16,16);
					return;
				case MESSAGE:
					iconPath = CaeClipConfiguration.getInstance().getSProp(CaeClipConfiguration.MAIL_ICON);
					img = Toolkit.getDefaultToolkit().getImage(iconPath);
					break;
			}
			
			final Image otherIcon = img;
			
			if (iconNumber != DEFAULT) 
			{
				
				
				swicherThread = new Thread(new Runnable(){

					public void run() {
						
						try {
							while(true)
							{
								icon.setImage(otherIcon,16,16);
								Thread.sleep(300);
								icon.setImage(defaultIcon,16,16);
								Thread.sleep(300);
							}
						} 
						catch (Exception e) 
						{ 
							logger.debug(e, e);
						}
					}
					
				});
				swicherThread.start();
				
			}
			
		} catch (Exception e) {
			// TODO
		}

	}
	
	
}
